package com.xsomnus.grpc.provider;

import com.xsomnus.grpc.lib.GreeterGrpc;
import com.xsomnus.grpc.lib.GreeterOuterClass;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.server.service.GrpcService;


/**
 * @author @xsomnus666_xiawenye★
 * @since 2019/7/3 0003 9:45
 * - 才需学也,学需静也/非淡泊无以明志，非宁静无以致远
 */
@Slf4j
@GrpcService
public class GreeterService extends GreeterGrpc.GreeterImplBase {

    @Override
    public void sayHello(GreeterOuterClass.HelloRequest request, StreamObserver<GreeterOuterClass.HelloReply> responseObserver) {
        if (request.getName().equals("1")) {
            throw new StatusRuntimeException(Status.Code.INTERNAL.toStatus().withDescription("测试异常"));
        }
        String message = "Hello " + request.getName();
        final GreeterOuterClass.HelloReply.Builder replyBuilder = GreeterOuterClass.HelloReply.newBuilder().setMessage(message);
        responseObserver.onNext(replyBuilder.build());
        responseObserver.onCompleted();
    }
}
